package com.ferrlo.test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import org.onlineframework.commons.util.MD5Util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ferrlo.model.Admin;
import com.ferrlo.model.Project;
import com.ferrlo.model.User;
import com.ferrlo.service.IAdminManager;
import com.ferrlo.service.IProjectManager;
import com.ferrlo.service.IUserManager;
import com.ferrlo.service.impl.ProjectManager;
import com.ferrlo.service.impl.UserManager;
import com.ferrlo.utils.SpringUtils;

public class testHibernate extends SpringUtils{

	@Test
	public void test() {

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
	}
	
	@Test
	public void testConnection(){
		Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory factory = configuration.buildSessionFactory();
		Session session = factory.openSession();
		Transaction transaction = session.beginTransaction();
	
		transaction.commit();
		session.close();
	}

	@Test
	public void insert() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"/spring/applicationContext-model.xml");
		Configuration configuration = new Configuration().configure();
		SessionFactory sessions = configuration.buildSessionFactory();
		Session session = sessions.openSession();
		Transaction transaction = session.beginTransaction();

		Project project = new Project();
		project.setPName("project1");
		project.setPStartTime(new Date());
		project.setPEndTime(new Date());
		
		session.save(project);

		transaction.commit();
		session.close();
	}

	@Test
	public void query(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		Configuration configuration = new Configuration().configure();
		SessionFactory sessions = configuration.buildSessionFactory();
		Session session = sessions.openSession();
//		IProjectManager manager = (IProjectManager) applicationContext.getBean("projectManager");
		IProjectManager manager = new ProjectManager();
		
		String dqQuery = "from Project";
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("UName", "project1");
		
		System.out.println("manager" + manager);
	
		
		session.close();	
		
	}
	
	
	@Test
	public void insertUser(){
		IUserManager userManager = (IUserManager) applicationContext.getBean("userManager");
		User user = new User();
		user.setUName("calt");
		user.setUNickname("calt");
		user.setUPwd(MD5Util.getMD5("lxyang"));
		user.setURegTime(new Date());
		
		userManager.save(user);
	}
	
	@Test
	public void insertAdmin(){
		IAdminManager adminManager = (IAdminManager) applicationContext.getBean("adminManager");
		Admin admin = new Admin();
		admin.setAName("admin");
		admin.setAPwd(MD5Util.getMD5("lxyang"));

		adminManager.save(admin);
	}
	
}
